/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.gate.service;

import org.apache.commons.lang.StringUtils;
import org.cshoo.tattoo.gate.entity.Authority;
import org.cshoo.tattoo.gate.entity.User;
import org.cshoo.tattoo.gate.repository.IAuthorityRepository;
import org.cshoo.tattoo.gate.repository.IUserRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Service
public class UserService {

    public static final String ADMIN_USERNAME = "admin";

    @Value("${admin-password:$2a$10$cEI9Fzuk6ZDmQg.Kd/Sb5uUSRirZUkSV.vi4ebJ1boYN63D6GOxmu}")
    private String adminPassword;

    @Resource
    private IUserRepository userRepository;

    @Resource
    private IAuthorityRepository authorityRepository;

    public User findByUsername(String username) {
        if (StringUtils.equals(username, ADMIN_USERNAME)) {
            User adminUser = new User();
            adminUser.setId(0L);
            adminUser.setUsername(ADMIN_USERNAME);
            adminUser.setPassword(adminPassword);
            return adminUser;
        }
        return userRepository.findUserByUsername(username);
    }

    /**
     * 查询权限
     *
     * @param userId 用户id
     * @return 权限
     */
    public List<Authority> findAuthoritiesByUserId(Long userId) {
        return authorityRepository.findAuthoritiesByUserId(userId);
    }

}
